home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / networking / cisco / account.shar / nameipacct_vms.c < prev    next >
C/C++ Source or Header  |  1992-08-07  |  2KB  |  104 lines

  1. /* ipname.c
  2.  *
  3.  * portable across unix/ultrix & vms systems
  4.  * first version (90/06/20) by Daniel Karrenberg (dfk@cwi.nl)
  5.  * this version (91/07/10) by Davide Salomoni (salomoni@cnaf.infn.it)
  6.  * to be linked on VMS systems with the appropriate socket library.
  7.  * Usage: ipname [secs]
  8.  * [secs] are the max seconds to wait for the gethostbyname() function
  9.  * to return (default=DEFSEC).
  10.  */
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <ctype.h>
  15. #include <netdb.h>
  16. #include <signal.h>
  17. #include <setjmp.h>
  18.  
  19. #ifdef VAXC
  20. #include <socket.h>
  21. #include <in.h>
  22. #else
  23. #include <sys/socket.h>
  24. #include <netinet/in.h>
  25. #endif
  26.  
  27. #define CACHE 50    /* dimension of the cyclic cache (don't exaggerate!) */
  28. #define DEFSEC 15   /* max seconds to wait for gethostbyname() to return */
  29.  
  30. char *giveme(char *string);
  31. int timeout();
  32.  
  33. unsigned sec=0;
  34. jmp_buf env;
  35.  
  36. struct entry {
  37.     int addr;
  38.     char name[40];
  39. };
  40. static struct entry *p[CACHE];
  41.  
  42. main(int argc, char **argv)
  43. {
  44.     char l[81],from[40],to[40];
  45.     unsigned int packets,bytes;
  46.  
  47.     if (argc>1) sec=atoi(argv[1]);
  48.     if (!sec) sec=DEFSEC;
  49.     signal(SIGALRM,timeout);
  50.  
  51.     while (fgets(l, sizeof(l), stdin) != NULL) {
  52.         if (isdigit(l[0])) {
  53.             sscanf(l, "%s %s %d %d\n", from, to, &packets, &bytes);
  54.             printf("%-29.29s %-29.29s%8d%10d K\n", 
  55.                 giveme(from), giveme(to), packets, bytes/1024);
  56.         }
  57.         else
  58.             fputs(l, stdout);
  59.     }
  60.     exit(0);
  61. }
  62.  
  63.  
  64. char *giveme(char *string)
  65. {
  66.     struct in_addr ha;
  67.     struct hostent *h;
  68.     register int j;
  69.     static int i=0,max=0;
  70.     int l;
  71.  
  72.     ha.s_addr=inet_addr(string);
  73.     for (j=0;j<max;j++)
  74.         if (ha.s_addr==p[j]->addr)
  75.             return(strcpy(string,p[j]->name));  /* cache hit */
  76.  
  77.     /* cache miss */
  78.     if (p[i]==NULL) 
  79.         if ((p[i]=(struct entry *)malloc(sizeof(struct entry)))==NULL) {
  80.             fprintf(stderr,"ipname: memory allocation error\n");
  81.             exit(1);
  82.         }
  83.     p[i]->addr=ha.s_addr;
  84.  
  85.     alarm(sec);
  86.     if (!setjmp(env) && (h=gethostbyaddr(&ha, 4, AF_INET))!=NULL) {
  87.         l=strlen(h->h_name);
  88.         strcpy(p[i]->name,l>29?h->h_name+(l-29):h->h_name);
  89.         strcpy(string,p[i]->name);
  90.     } else strcpy(p[i]->name,string);
  91.     alarm(0);
  92.  
  93.     i = ++i % CACHE;
  94.     if (max<CACHE) max++;
  95.     return(string);
  96. }
  97.  
  98.  
  99. int timeout()
  100. {
  101.     alarm(0);
  102.     longjmp(env,1);
  103. }
  104.